<?php

require_once 'adodb5/adodb-errorhandler.inc.php';
require_once 'adodb5/adodb.inc.php';

abstract class Connection_Mysql {

    private $_conn_adodb, $_count = 1;

    /**
     * 
     * @param type $dbtype
     * @param type $host
     * @param type $username
     * @param type $pass
     * @param type $dbname
     * 
     */
    public function __construct($host = "", $username = "", $pass = "", $dbname = "", $dbtype = "mysql") {
        try {
            $this->_conn_adodb = &ADONewConnection($dbtype);
            $this->_conn_adodb->Connect($host, $username, $pass, $dbname);
            $this->_conn_adodb->execute("set names utf8");
        } catch (Exception $exc) {
            die("Connection Fail...");
        }
    }

    function __destruct() {
        $this->_conn_adodb->close();
    }

    /**
     * 
     * @param array $values
     * @param type $table
     * 
     */
    public function insert($table, array $values) {
        try {
            $query = "INSERT INTO $table SET ";
            $count = count($values);
            foreach ($values as $key => $value) {
                if ($this->_count == $count)
                    $query .= "$key='{$value}' ";
                else
                    $query .= "$key='{$value}', ";
                $this->_count++;
            }
            $this->_conn_adodb->execute($query);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die("Error!!!");
        }
    }

    /**
     * 
     * @param type $table
     * @param array $values
     * @param type $id
     * 
     */
    public function update($table, array $values, $id) {
        try {
            $query = "UPDATE $table SET ";
            $count = count($values);

            foreach ($values as $key => $value) {
                if ($this->_count == $count)
                    $query .= "$key='{$value}' ";
                else
                    $query .= "$key='{$value}', ";
                $this->_count++;
            }
            $query .= "WHERE id='{$id}'";
            return $this->_conn_adodb->execute($query);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die("Error!!!");
        }
    }

    /**
     * 
     * @param type $table
     * @param type $option_id
     * 
     */
    public function delete($table, $option_id) {
        try {
            $query = "DELETE FROM $table FROM id='{$option_id}'";
            return $this->_conn_adodb->execute($query);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die("Error!!!");
        }
    }

    /**
     * 
     * @param array $query
     * 
     */
    public function find($query) {
        try {
            $rs = $this->_conn_adodb->execute($query);
            return $rs->getrows();
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die("Error!!!");
        }
    }
    
    /**
     * 
     * @return type
     * 
     */
    public function getId() {
        try {
            return $this->_conn_adodb->Insert_Id();
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die("Error!!!");
        }
    }

}

class Mysql extends Connection_Mysql {

    public function __construct() {
        parent::__construct(host, username, password, database, type);
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
